package org.jivesoftware.smack.util;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

public class Base32Encoder
  implements StringEncoder
{
  private static Base32Encoder instance = new Base32Encoder();
  private static final String ALPHABET = "ABCDEFGHIJKLMNOPQRSTUVWXYZ2345678";

  public static Base32Encoder getInstance()
  {
    return instance;
  }

  public String decode(String paramString)
  {
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    byte[] arrayOfByte1 = paramString.getBytes();
    for (int i = 0; i < arrayOfByte1.length; i++)
    {
      char c = (char)arrayOfByte1[i];
      if (!Character.isWhitespace(c))
      {
        c = Character.toUpperCase(c);
        localByteArrayOutputStream.write((byte)c);
      }
    }
    while (localByteArrayOutputStream.size() % 8 != 0)
      localByteArrayOutputStream.write(56);
    byte[] arrayOfByte2 = localByteArrayOutputStream.toByteArray();
    localByteArrayOutputStream.reset();
    DataOutputStream localDataOutputStream = new DataOutputStream(localByteArrayOutputStream);
    for (int j = 0; j < arrayOfByte2.length / 8; j++)
    {
      short[] arrayOfShort = new short[8];
      int[] arrayOfInt = new int[5];
      int k = 8;
      int n;
      for (int m = 0; m < 8; m++)
      {
        n = (char)arrayOfByte2[(j * 8 + m)];
        if (n == 56)
          break;
        arrayOfShort[m] = (short)"ABCDEFGHIJKLMNOPQRSTUVWXYZ2345678".indexOf(arrayOfByte2[(j * 8 + m)]);
        if (arrayOfShort[m] < 0)
          return null;
        k--;
      }
      m = paddingToLen(k);
      if (m < 0)
        return null;
      arrayOfInt[0] = (arrayOfShort[0] << 3 | arrayOfShort[1] >> 2);
      arrayOfInt[1] = ((arrayOfShort[1] & 0x3) << 6 | arrayOfShort[2] << 1 | arrayOfShort[3] >> 4);
      arrayOfInt[2] = ((arrayOfShort[3] & 0xF) << 4 | arrayOfShort[4] >> 1 & 0xF);
      arrayOfInt[3] = (arrayOfShort[4] << 7 | arrayOfShort[5] << 2 | arrayOfShort[6] >> 3);
      arrayOfInt[4] = ((arrayOfShort[6] & 0x7) << 5 | arrayOfShort[7]);
      try
      {
        for (n = 0; n < m; n++)
          localDataOutputStream.writeByte((byte)(arrayOfInt[n] & 0xFF));
      }
      catch (IOException localIOException)
      {
      }
    }
    return new String(localByteArrayOutputStream.toByteArray());
  }

  public String encode(String paramString)
  {
    byte[] arrayOfByte = paramString.getBytes();
    ByteArrayOutputStream localByteArrayOutputStream = new ByteArrayOutputStream();
    for (int i = 0; i < (arrayOfByte.length + 4) / 5; i++)
    {
      short[] arrayOfShort = new short[5];
      int[] arrayOfInt = new int[8];
      int j = 5;
      for (int k = 0; k < 5; k++)
        if (i * 5 + k < arrayOfByte.length)
        {
          arrayOfShort[k] = (short)(arrayOfByte[(i * 5 + k)] & 0xFF);
        }
        else
        {
          arrayOfShort[k] = 0;
          j--;
        }
      k = lenToPadding(j);
      arrayOfInt[0] = (byte)(arrayOfShort[0] >> 3 & 0x1F);
      arrayOfInt[1] = (byte)((arrayOfShort[0] & 0x7) << 2 | arrayOfShort[1] >> 6 & 0x3);
      arrayOfInt[2] = (byte)(arrayOfShort[1] >> 1 & 0x1F);
      arrayOfInt[3] = (byte)((arrayOfShort[1] & 0x1) << 4 | arrayOfShort[2] >> 4 & 0xF);
      arrayOfInt[4] = (byte)((arrayOfShort[2] & 0xF) << 1 | arrayOfShort[3] >> 7 & 0x1);
      arrayOfInt[5] = (byte)(arrayOfShort[3] >> 2 & 0x1F);
      arrayOfInt[6] = (byte)((arrayOfShort[3] & 0x3) << 3 | arrayOfShort[4] >> 5 & 0x7);
      arrayOfInt[7] = (byte)(arrayOfShort[4] & 0x1F);
      for (int m = 0; m < arrayOfInt.length - k; m++)
      {
        int n = "ABCDEFGHIJKLMNOPQRSTUVWXYZ2345678".charAt(arrayOfInt[m]);
        localByteArrayOutputStream.write(n);
      }
    }
    return new String(localByteArrayOutputStream.toByteArray());
  }

  private static int lenToPadding(int paramInt)
  {
    switch (paramInt)
    {
    case 1:
      return 6;
    case 2:
      return 4;
    case 3:
      return 3;
    case 4:
      return 1;
    case 5:
      return 0;
    }
    return -1;
  }

  private static int paddingToLen(int paramInt)
  {
    switch (paramInt)
    {
    case 6:
      return 1;
    case 4:
      return 2;
    case 3:
      return 3;
    case 1:
      return 4;
    case 0:
      return 5;
    case 2:
    case 5:
    }
    return -1;
  }
}

/* Location:           D:\hygieia\userclient\common\libs\easemobchat_2.1.8.jar
 * Qualified Name:     org.jivesoftware.smack.util.Base32Encoder
 * JD-Core Version:    0.6.1
 */